package com.tzq.SERVLET;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.connector.Request;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.sun.mail.iap.Response;
import com.tzq.JAVABEAN.ConnectDB;

/**
 * Servlet implementation class GetFriend
 */
public class GetFriend extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetFriend() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int method = Integer.parseInt(request.getParameter("method"));
		if(method==0){
			MyFriend(request, response);
		}
		if(method==1){
			Friendapply(request, response);
		}
	}
	
	public void MyFriend(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{   
			response.setContentType("text/html;charset=UTF-8"); 
			PrintWriter out = response.getWriter();
		    String sessionidString = request.getParameter("sessionid");
			ConnectDB sql = new ConnectDB();
			ResultSet rsFriend = sql.executeQuery("select UserName,UserID,head from Friends,Userinfo where MyID="+sessionidString+" and State=1 and FriendID=UserID");
			
			JSONObject json = new JSONObject();  
	        JSONArray array = new JSONArray();  
	        JSONObject member = null;  
		    while(rsFriend.next()){
		    	member = new JSONObject();  
		    	member.put("UserID", rsFriend.getString("UserID"));
	            member.put("UserName", rsFriend.getString("UserName"));
	            member.put("head", rsFriend.getString("head"));
	            array.add(member);  
		    }
		    sql.closeConnection();
		    json.put("myfirend", array);
		    out.print(json.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public void Friendapply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			response.setContentType("text/html;charset=UTF-8"); 
			PrintWriter out = response.getWriter();
		    String sessionidString = request.getParameter("sessionid");
			ConnectDB sql = new ConnectDB();
			ResultSet rsFriendApply = sql.executeQuery("select UserName,MyID,head from Friends,Userinfo where FriendID="+sessionidString+" and State=0 and MyID=UserID");
			
			JSONObject json = new JSONObject();  
	        JSONArray array = new JSONArray();  
	        JSONObject member = null;  
		    while(rsFriendApply.next()){
		    	member = new JSONObject();
	            member.put("UserName", rsFriendApply.getString("UserName"));
	            member.put("head", rsFriendApply.getString("head"));
	            member.put("myid", rsFriendApply.getString("MyID"));
	            array.add(member);  
		    }
		    sql.closeConnection();
		    json.put("friendapply", array);
		    out.print(json.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
	
	}
}
